package com.handycraftz.site.client.presenter;

import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.event.shared.HandlerManager;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.ui.HasWidgets;
import com.handycraftz.site.client.event.HandyCraftzEvent;
import com.handycraftz.site.client.event.HandyCraftzEventHandler;
import com.handycraftz.site.shared.util.HandyCraftzConstants;

public class AppPresenter extends Presenter implements
		ValueChangeHandler<String> {

	private LayoutPresenter layoutPresenter;
	private HasWidgets container;

	public AppPresenter(HandlerManager eventBus) {
		super(eventBus);
	}

	private void bindViewSwitchers() {

		History.addValueChangeHandler(this);

		eventBus.addHandler(HandyCraftzEvent.TYPE,
				new HandyCraftzEventHandler() {

					@Override
					public void onFired(HandyCraftzEvent handyCraftzEvent) {

						if (handyCraftzEvent.getToken().equals(
								HandyCraftzConstants.HANDYCRAFTZ_TOKEN_HOME)) {
							addToHistory(
									HandyCraftzConstants.HANDYCRAFTZ_TOKEN_HOME,
									true);
						} else if (handyCraftzEvent
								.getToken()
								.equals(HandyCraftzConstants.HANDYCRAFTZ_TOKEN_ABOUT_US)) {
							addToHistory(
									HandyCraftzConstants.HANDYCRAFTZ_TOKEN_ABOUT_US,
									true);
						} else if (handyCraftzEvent
								.getToken()
								.equals(HandyCraftzConstants.HANDYCRAFTZ_TOKEN_PRODUCTS)) {
							addToHistory(
									HandyCraftzConstants.HANDYCRAFTZ_TOKEN_PRODUCTS,
									true);
						} else if (handyCraftzEvent.getToken().equals(
								HandyCraftzConstants.HANDYCRAFTZ_TOKEN_BLOG)) {
							addToHistory(
									HandyCraftzConstants.HANDYCRAFTZ_TOKEN_BLOG,
									true);
						} else if (handyCraftzEvent.getToken().equals(
								HandyCraftzConstants.HANDYCRAFTZ_TOKEN_JOIN_US)) {
							addToHistory(
									HandyCraftzConstants.HANDYCRAFTZ_TOKEN_JOIN_US,
									true);
						} else if (handyCraftzEvent
								.getToken()
								.equals(HandyCraftzConstants.HANDYCRAFTZ_TOKEN_CONTACT_US)) {
							addToHistory(
									HandyCraftzConstants.HANDYCRAFTZ_TOKEN_CONTACT_US,
									true);
						} else {
							addToHistory(
									HandyCraftzConstants.HANDYCRAFTZ_TOKEN_LAYOUT,
									true);
						}
					}
				});
	}

	private void addToHistory(String historyToken, boolean issueEvent) {
		History.newItem(historyToken, issueEvent);
	}

	@Override
	public void onValueChange(ValueChangeEvent<String> event) {
		Presenter presenter = null;
		String token = event.getValue().trim();
		if (token != null && !token.equals("")) {
			if (token.equals(HandyCraftzConstants.HANDYCRAFTZ_TOKEN_LAYOUT)) {
				layoutPresenter = new LayoutPresenter(this.eventBus);
				layoutPresenter.go(container);
				LayoutPresenter.startSlider();
			} else if (token
					.equals(HandyCraftzConstants.HANDYCRAFTZ_TOKEN_HOME)) {
				presenter = new ContentPresenter(this.eventBus);					
			}
		}
	}

	@Override
	public void go(HasWidgets container) {
		this.container = container;
		addToHistory(HandyCraftzConstants.HANDYCRAFTZ_TOKEN_LAYOUT, true);
		bindViewSwitchers();
		History.fireCurrentHistoryState();
	}

}
